package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;

/**
 * @Classname MenuController
 * @Description
 * @Date 2020/8/30 10:27 上午
 * @Created by zhaoeng
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    //删除菜单
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            Menu menu = menuService.findById(id);
            List<Menu> children = menu.getChildren();
            // 判断是否有子菜单
            if (children != null && children.size() > 0) {
                return new Result(false, "菜单含有子菜单,禁止删除");
            }
            // 删除和角色的关联关系
            menuService.deleteAssociation(id);
            menuService.delete(id);
            return new Result(true, "删除菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除菜单失败,请检查是否有子菜单");
        }
    }

        //查询一级菜单
    @RequestMapping("/findlevel1Menu")
    public Result findlevel1Menu() {
        try {
          List<Menu> menus= menuService.findlevel1Menu();
            Menu menu = new Menu();
            menu.setId(99999);
            menu.setName("无父级菜单");
            menus.add(menu);

            return new Result(true, "查找一级菜单成功",menus);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查找一级菜单失败");
        }
    }
   //添加菜单
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu) {
        try {
            if (menu==null){
                return new Result(false, "添加失败");
            }
            Integer parentMenuId = menu.getParentMenuId();
            if (parentMenuId!=null&&parentMenuId.equals(99999)){
                menu.setParentMenuId(null);
                menu.setLevel(1);
            }
            if (parentMenuId!=null&&!parentMenuId.equals(99999)){
                menu.setLevel(2);
            }
            if (parentMenuId==null){
                menu.setLevel(1);
            }
            menuService.add(menu);
            return new Result(true, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加失败");
        }
    }
    //根据id查菜单
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {

            Menu menu=menuService.findById(id);
            return new Result(true, "查父菜单成功成功",menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加失败");
        }
    }
    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu) {
        try {

          if (menu==null){
                return new Result(false, "添加失败");
            }
            if (menu.getLevel()==1){
                List<Menu> menus= menuService.findChildrenByParentId(menu.getId());
                if (menus!=null&&menus.size()>0){
                    ArrayList<String> name = new ArrayList<>();
                    for (Menu menu1 : menus) {
                        String name1 = menu1.getName();
                        name.add(name1);
                    }

                    return new Result(false, name.toString());
                }
            }
            Integer parentMenuId = menu.getParentMenuId();
            if (parentMenuId!=null&&parentMenuId.equals(99999)){
                menu.setParentMenuId(null);
                menu.setLevel(1);
            }
            if (parentMenuId!=null&&!parentMenuId.equals(99999)){
                menu.setLevel(2);
            }
            if (parentMenuId==null){
                menu.setLevel(1);
            }


            menuService.edit(menu);
            return new Result(true, "编辑菜单成功成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "编辑失败");
        }
    }
    @RequestMapping("/pageQuery")
    public PageResult pageQuery(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult =menuService.pageQuery(queryPageBean.getQueryString(),
                queryPageBean.getPageSize(),
                queryPageBean.getCurrentPage());
        return pageResult;
    }
}
